﻿using System;
using System.Collections.Generic;

namespace Gum.FizzBuzz
{
    /// <summary>
    /// Default implementation of IFizzBuzzJobRunner
    /// </summary>
    public class FizzBuzzJobRunner : IFizzBuzzJobRunner
    {
        /// <summary>
        /// Runs a FizzBuzz job
        /// </summary>
        /// <param name="job">The job to run</param>
        /// <param name="stringWriter">The writer to send the output to</param>
        public void Run(IFizzBuzzJob job, IStringWriter stringWriter)
        {
            if (null == job) throw new ArgumentNullException("job");
            if (null == stringWriter) throw new ArgumentNullException("stringWriter");

            var countUp = job.Start <= job.End;

            if (countUp)
            {
                for (var i = job.Start; i <= job.End; i++)
                    stringWriter.Write(job.Transform(i));
            }
            else
            {
                for (var i = job.Start; i >= job.End; i--)
                    stringWriter.Write(job.Transform(i));
            }
        }

        public IEnumerable<string> Run(IFizzBuzzJob job)
        {
            if (null == job) throw new ArgumentNullException("job");

            var countUp = job.Start <= job.End;

            if (countUp)
            {
                for (var i = job.Start; i <= job.End; i++)
                    yield return job.Transform(i);
            }
            else
            {
                for (var i = job.Start; i >= job.End; i--)
                    yield return job.Transform(i);
            }
        }
    }
}